package com.example.jack.community.view.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.percent.PercentRelativeLayout;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.BaseActivity;
import com.example.jack.community.R;
import com.example.jack.community.data.loginbean.Code;
import com.example.jack.community.data.loginbean.User;
import com.example.jack.community.model.login.RegisterView;
import com.example.jack.community.persenter.login.RegisterPresenterlmp;
import com.example.jack.community.view.login.util.CountDownTimerUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * 注册页面
 */
public class RegisterActivity extends BaseActivity implements RegisterView {

    /**
     * 请输入手机号输入框
     */
    @BindView(R.id.edt_password_a)
    EditText edtPasswordA;

    /**
     * 请输入验证码输入框
     */
    @BindView(R.id.edt_password_b)
    EditText edtPasswordB;

    /**
     * 请输入密码对话框
     */
    @BindView(R.id.edt_password_c)
    EditText edtPasswordC;

    /**
     * 请确认密码对话框
     */
    @BindView(R.id.edt_password_d)
    EditText edtPasswordD;

    /**
     * 确认注册
     */
    @BindView(R.id.register_button)
    Button registerButton;

    /**
     * 服务条款
     */
    @BindView(R.id.server_text)
    TextView serverText;

    /**
     * 获取验证码按钮文字
     */
    @BindView(R.id.refister_getcode)
    TextView refisterGetcode;

    /**
     * 注册实现接口
     */
    private RegisterPresenterlmp registerpresenterimp = new RegisterPresenterlmp(RegisterActivity.this);

    /**
     * 短信验证回调
     */
    private EventHandler eventHandler;

    private static final String TAG = "logins";

    private boolean a = false;

    //注册成功
    private SweetAlertDialog sweetAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity_register);
        ButterKnife.bind(this);
        registerpresenterimp.attachView(this);
        //判断验证码是否正确的回调
        code();
        // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);
    }

    private void code() {
        // 创建EventHandler对象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable) data;
                    final String msg = throwable.getMessage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Gson gson = new Gson();

                            Code shopInfo = gson.fromJson(msg, Code.class);

                            Log.d(TAG, "onResponse: " + shopInfo.getStatus() + msg);

                            if (shopInfo.getStatus() == 468) {
                                Toast.makeText(RegisterActivity.this, "输入的验证码错误,请重新输入", Toast.LENGTH_SHORT).show();
                            }
                            if (shopInfo.getStatus() == 603) {
                                Toast.makeText(RegisterActivity.this, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
                            }
                            if (shopInfo.getStatus() == 477) {
                                Toast.makeText(RegisterActivity.this, "当前手机号发送短信的数量超过限额", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        // 处理你自己的逻辑
                        Log.d(TAG, "afterEvent: 获取成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegisterActivity.this, "验证码获取成功,请注意查收", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提价验证码
                        Log.d(TAG, "afterEvent: 验证成功");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String phone = edtPasswordA.getText().toString().trim();
                                String password_two = edtPasswordD.getText().toString().trim();
                                String password_one = edtPasswordC.getText().toString().trim();
                                User user = new User(phone, password_one, password_two);
                                registerpresenterimp.register(user, password_one, password_two);
                            }
                        });
                    }
                }
            }
        };
    }

    @OnClick({R.id.register_accept, R.id.register_return, R.id.refister_getcode, R.id.register_button, R.id.server_text})
    public void onViewClicked(View view) {
        String mPhone = edtPasswordA.getText().toString().trim();
        switch (view.getId()) {
            case R.id.register_accept://接受注册协议
                if (a == false) {
//                    registerAccept.setImageResource(R.drawable.register_accept);
                    a = true;
                } else {
//                    registerAccept.setImageResource(R.drawable.acceptpress);
                    a = false;
                }
                break;
            case R.id.register_return://返回登录界面
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                break;
            case R.id.refister_getcode://获取验证码
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(refisterGetcode, 60000, 1000);
                mCountDownTimerUtils.start();
                SMSSDK.getVerificationCode("86", mPhone);
                break;
            case R.id.server_text://跳转到服务使用条款
                startActivity(new Intent(RegisterActivity.this, ServiceActivity.class));
                break;
            case R.id.register_button://确认注册按钮
                String code = edtPasswordB.getText().toString().trim();
                SMSSDK.submitVerificationCode("86", mPhone, code);
                break;
        }
    }

    @Override
    public void registerSuccess(String msg) {
        new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText(msg)
                .setContentText("Login immediately")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                    }
                })
                .show();
    }

    @Override
    public void registerFail(String msg) {
        new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                .setTitleText(msg)
                .setContentText("I'm sorry, please re-enter")
                .show();
        Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
    }

    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }
}
